﻿using Admin.ApplicationLib.Entities;
using Admin.CommonLib.Models.Permission;
using Ardalis.Specification;

namespace Admin.ApplicationLib.Specifications
{
    public sealed class PermissionSpecification : Specification<PermissionEntity>
    {
        public PermissionSpecification(PermissionSearchRequestDto model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            // 1 查询Menu以及Url
            if (model.TreeType == 1)
            {
                Query.Where(i => !string.IsNullOrEmpty(i.Type) &&
                                 (i.Type.Equals("Menu") || i.Type.Equals("Url")));
            }

            if (model.Page > 0)
            {
                Query.Skip((model.Page - 1) * model.Limit);
            }

            if (model.Limit > 0)
            {
                Query.Take(model.Limit);
            }

            Query.OrderBy(i => i.Sort.ToString());
        }
    }
}